﻿using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Diagnostics;
using System.Text.RegularExpressions;

namespace GISAlgorithm1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("若要进行图像压缩，请输入\"1\",若要进行图像还原，请输入\"2\"");
            int i = Convert.ToInt32(Console.ReadLine());

            //压缩图像
            if (i == 1)
            {
                Console.WriteLine("请输入输入图像的文件路径（PNG格式）：");
                string inputImagePath = Console.ReadLine();

                Console.WriteLine("请输入输出图像的文件路径（PNG格式）：");
                string outputImagePath = Console.ReadLine();

                try
                {
                    using (Bitmap inputBitmap = new Bitmap(inputImagePath))
                    using (Bitmap outputBitmap = new Bitmap(inputBitmap.Width, inputBitmap.Height))
                    {

                        Color avgPixelColor = CalculateAverageColor(inputBitmap);


                        for (int y = 0; y < inputBitmap.Height; y++)
                        {
                            for (int x = 0; x < inputBitmap.Width; x++)
                            {
                                Color currentPixelColor = inputBitmap.GetPixel(x, y);

                                int diffR = Math.Max(0, Math.Min(255, currentPixelColor.R - avgPixelColor.R));
                                int diffG = Math.Max(0, Math.Min(255, currentPixelColor.G - avgPixelColor.G));
                                int diffB = Math.Max(0, Math.Min(255, currentPixelColor.B - avgPixelColor.B));

                                Color diffColor = Color.FromArgb(diffR, diffG, diffB);
                                outputBitmap.SetPixel(x, y, diffColor);
                            }
                        }

                        outputBitmap.Save(outputImagePath, ImageFormat.Png);
                        Console.WriteLine("图像的平均RGB值为{0},{1},{2}",Convert.ToString(avgPixelColor.R),Convert.ToString(avgPixelColor.G),Convert.ToString(avgPixelColor.B));
                        Console.WriteLine("差分映射编码完成，已保存到 " + outputImagePath);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("发生错误: " + ex.Message);
                }
            }

            //还原图像
            else
            {
                Console.WriteLine("请输入输入图像的文件路径（PNG格式）：");
                string inputImagePath = Console.ReadLine();

                Console.WriteLine("请输入输出图像的文件路径（PNG格式）：");
                string outputImagePath = Console.ReadLine();

                try
                {
                    using (Bitmap inputBitmap = new Bitmap(inputImagePath))
                    using (Bitmap outputBitmap = new Bitmap(inputBitmap.Width, inputBitmap.Height))
                    {
                        Color avgPixelColor = CalculateAverageColor(inputBitmap);
                        for (int y = 0; y < inputBitmap.Height; y++)
                        {
                            for (int x = 0; x < inputBitmap.Width; x++)
                            {
                                Color currentPixelColor = inputBitmap.GetPixel(x, y);

                                int diffR = Math.Max(0, Math.Min(255, currentPixelColor.R + avgPixelColor.R));
                                int diffG = Math.Max(0, Math.Min(255, currentPixelColor.G + avgPixelColor.G));
                                int diffB = Math.Max(0, Math.Min(255, currentPixelColor.B + avgPixelColor.B));

                                Color diffColor = Color.FromArgb(diffR, diffG, diffB);
                                outputBitmap.SetPixel(x, y, diffColor);
                            }
                        }

                        outputBitmap.Save(outputImagePath, ImageFormat.Png);
                        Console.WriteLine("还原图像完成，已保存到 " + outputImagePath);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("发生错误: " + ex.Message);
                }
            }
        }


        /// <summary>
        /// 计算图像各通道平均像元值
        /// </summary>
        /// <param name="inputBitmap"></param>
        /// <returns></returns>
        static Color CalculateAverageColor(Bitmap inputBitmap)
        {
            double[] a = new double[3] { 0, 0, 0 };
            Color curColor;
            double RR = 0;
            double GG = 0;
            double BB = 0;
            for (int y3 = 0; y3 < inputBitmap.Height; y3++)
            {
                for (int x3 = 0; x3 < inputBitmap.Width; x3++)
                {
                    curColor = inputBitmap.GetPixel(x3, y3);
                    RR = RR + curColor.R;
                    GG = GG + curColor.G;
                    BB = BB + curColor.B;
                }
            }
            a[0] = Math.Round(RR / (inputBitmap.Width * inputBitmap.Height), 2);
            a[1] = Math.Round(GG / (inputBitmap.Width * inputBitmap.Height), 2);
            a[2] = Math.Round(BB / (inputBitmap.Width * inputBitmap.Height), 2);
            Color avgPixelColor = Color.FromArgb((int)a[0], (int)a[1], (int)a[2]);
            return Color.FromArgb((int)a[0], (int)a[1], (int)a[2]);
        }



        }
    }
